package com.demo.forum.service2;


import com.demo.forum.model2.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;


/**
 * 活期存款业务
 */
@Service
@Slf4j
public class SavingsDeposit implements IDeposit{
    private double interest;//到期利息
    public Calendar calendar  = Calendar.getInstance();

    @Override
    @Transactional
    public void saveAmount(Double money, Integer month,User user) {
        if(money <= 0){
                log.warn("存款不合法!");
                return;
        }

        if(user.getDeleteState() ==1){
                log.warn("用户已经被冻结");
                 return;
        }

        Double balance = user.getBalance();//查询当前用户余额
        user.setBalance(balance + money);//更新用户余额
        //设置到期时间
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, month);
        user.setExpirationTime(calendar.getTime());
        interest(month,user);
        show(money,month,user);
    }

    @Override
    public void interest(Integer month,User user) {
        //设置利息
        if(month <= 0){
            System.out.println("参数校验失败!");
            return;
        }
        user.setInterest(user.getBalance()* 1.75/100 * month * 30 /360) ;
    }

    @Override
    public void show(Double money,Integer month,User user) {
        System.out.println("存款人姓名: "+user.getUsername() +
                ",账号 : "+user.getId() + ",存款金额 " + money +
                ",余额 : " + user.getBalance());
    }
}
